package com.yugao.fintech.libra.modules;

import cn.hutool.core.util.RandomUtil;
import com.yugao.fintech.libra.common.model.enums.BoolEnum;
import com.yugao.fintech.libra.system.model.entity.SysDept;
import com.yugao.fintech.libra.system.model.entity.SysUser;
import com.yugao.fintech.libra.system.service.SysDeptService;
import com.yugao.fintech.libra.system.service.SysRoleService;
import com.yugao.fintech.libra.system.service.SysUserService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;


@SpringBootTest
public class CreateData {
    @Autowired
    private SysUserService SysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysDeptService sysDeptService;

    @Test
    public void test() {
        // 为了方便测试, 所有id范围都是 1000 ~ 1001000
//        long startId = 1000L;
//        long endId = 1001000L;

        long startId = 1000L;
        long endId = 501000L;

        // 一般大公司也不会有非常多的部门和角色, 这里设置角色和部门都是5000个
        long deptAndRoleStartId = 1000L;
        long deptAndRoleEndId = 6000L;

        // 创建角色
        System.out.println("=================================> 创建角色");
//        for (long i = deptAndRoleStartId; i <= deptAndRoleEndId ; i++) {
//            if (i % 1000 == 0) {
//                System.out.println("create role num: " + i);
//            }
//            SysRolePO sysRolePO = new SysRolePO();
//            int random = RandomUtil.randomInt(1, 5);
//            if (random == 1) {
//                sysRolePO.setDataScope(RoleDataScopeEnum.ALL_DATA.getKey());
//            }
//            if (random == 2) {
//                sysRolePO.setDataScope(RoleDataScopeEnum.CUSTOM_DATA.getKey());
//            }
//            if (random == 3) {
//                sysRolePO.setDataScope(RoleDataScopeEnum.CURRENT_DEPT_DATA.getKey());
//            }
//            if (random == 4) {
//                sysRolePO.setDataScope(RoleDataScopeEnum.CURRENT_AND_CHILD_DEPT_DATA.getKey());
//            }
//            if (random == 5) {
//                sysRolePO.setDataScope(RoleDataScopeEnum.ONLY_MY_DATA.getKey());
//            }
//            sysRolePO.setId(i);
//            sysRolePO.setUserId(RandomUtil.randomLong(startId, endId));
//            List<Long> deptIds1 = new ArrayList<>();
//            deptIds1.add(RandomUtil.randomLong(deptAndRoleStartId, deptAndRoleEndId));
//            deptIds1.add(RandomUtil.randomLong(deptAndRoleStartId, deptAndRoleEndId));
//            deptIds1.add(RandomUtil.randomLong(deptAndRoleStartId, deptAndRoleEndId));
//            Long[] deptIds2 = new Long[deptIds1.size()];
//            sysRolePO.setDeptIds(deptIds1.toArray(deptIds2));
//            sysRolePO.setUserId(RandomUtil.randomLong(startId, endId));
//            sysRolePO.setDelFlag(CommonConstant.DEL_FLAG_0 + "");
//            sysRolePO.setRoleName("test-" + RandomUtil.randomNumbers(10));
//            sysRoleService.insertRole(sysRolePO);
//            sysRoleService.authDataScope(sysRolePO);
//        }

        // 创建部门
        System.out.println("=================================> 创建部门");
        for (long i = deptAndRoleStartId; i <= deptAndRoleEndId ; i++) {
            if (i % 1000 == 0) {
                System.out.println("create dept num: " + i);
            }
            SysDept sysDept = new SysDept();
            sysDept.setId(i);
            sysDept.setAncestors("1");
            sysDept.setParentId(1L);
            sysDept.setDelFlag(Integer.valueOf(BoolEnum.FALSE.getCode()));
            sysDept.setDeptName("test-" + RandomUtil.randomNumbers(16));
            sysDept.setSortNum(1);
            sysDept.setEmail(RandomUtil.randomNumbers(16));
            sysDept.setPhone(RandomUtil.randomNumbers(13));
            sysDeptService.saveDept(sysDept);
        }

        // 随机将角色和部门与用户关联次数
        System.out.println("=================================> 创建用户");
        for (long i = startId; i <= endId ; i++) {
            String random = RandomUtil.randomNumbers(20);
            List<Long> deptIds = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                deptIds.add(RandomUtil.randomLong(deptAndRoleStartId, deptAndRoleEndId));
            }

            List<Long> roleIds = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                roleIds.add(RandomUtil.randomLong(deptAndRoleStartId, deptAndRoleEndId));
            }
            if (i % 1000 == 0) {
                System.out.println("create user num: " + i);
            }

            SysUser sysUser = new SysUser();
            sysUser.setUserId(i);
            sysUser.setDeptIds(deptIds);
            sysUser.setRoleIds(roleIds);
            sysUser.setDelFlag(BoolEnum.FALSE.getCode());
            sysUser.setEmail(random);
            sysUser.setNickName(random);
            sysUser.setUserName("test-" + random);
//            sysUserService.saveUser(sysUser);
        }
    }
}
